package com.example.easyutils.dedecms;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.easyutils.BaseActivity;
import com.example.easyutils.R;
import com.example.easyutils.httputil.HttpGetRequest;
import com.example.easyutils.translation.TranslationActivity;
import com.example.easyutils.util.DialogLoad;
import com.example.easyutils.util.InputDel;
import com.example.easyutils.util.MyTypeAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;
import okhttp3.internal.Internal;

//仿站
public class Decms extends BaseActivity {
    private DialogLoad mDialogLoad;
    private static final int SUCCESS_CODE = 1,ERROR_CODE = 2,WARN_CODE = 3;
    private EditText editText;
    private TextView showText;
    private String dedecmsZip;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            mDialogLoad.dialogCancel();
            switch (msg.what){
                case SUCCESS_CODE:
                    showText.setText("仿站成功，前往下载 "+dedecmsZip);
                    //获取剪贴板管理器：
                    ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
// 创建普通字符型ClipData
                    ClipData mClipData = ClipData.newPlainText("Label", dedecmsZip);
// 将ClipData内容放到系统剪贴板里。
                    cm.setPrimaryClip(mClipData);
                    break;
                case ERROR_CODE:
                    Toast.makeText(Decms.this,"出错啦",Toast.LENGTH_LONG).show();
                    break;
                case WARN_CODE:
                    Toast.makeText(Decms.this,"网站不能复制或地址错误",Toast.LENGTH_LONG).show();
                    break;
            }
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dedecms);
        editText = (EditText)findViewById(R.id.dedecmsText);
        showText = (TextView) findViewById(R.id.showText);

        mDialogLoad = new DialogLoad(Decms.this);

        showText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse(dedecmsZip);
                Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                startActivity(intent);
            }
        });

        ImageView imageView = null;
        InputDel inputDel = new InputDel(editText,imageView, Decms.this);
        inputDel.initListener();
        inputDel.editDel();
    }


    public void getDedecmsZip(View v){//仿站
        mDialogLoad.dialogLoaded("仿站中...");
        String url = "";
        url = editText.getText().toString();
        if(url == ""){
            Toast.makeText(Decms.this,"地址不能为空！",Toast.LENGTH_LONG).show();
            return;
        }
        HttpGetRequest.sendOkHttpGetRequst("https://api.oioweb.cn/api/dedecms.php?url="+url,
                new okhttp3.Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        handler.sendEmptyMessage(ERROR_CODE);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new GsonBuilder()
                                .registerTypeAdapter(new TypeToken<Map<String, String>>(){

                                }.getType(),new MyTypeAdapter()).create();
                        Map<String,String> dataDetail = gson.fromJson(result,new TypeToken<Map<String,String>>(){

                        }.getType());

                        Log.d("dataDetail",dataDetail+"");
                        String code = dataDetail.get("code");
                        Log.d("code",code);
                        if(code.equals("1")){
                            dedecmsZip = dataDetail.get("url");
                            handler.sendEmptyMessage(SUCCESS_CODE);
                        }else{
                            handler.sendEmptyMessage(WARN_CODE);
                        }

                    }
                });
    }
}
